import networkx as nx
import numpy as np
import glob
import os, os.path
import math
import pickle
import scipy.sparse as sp
import random
from sklearn.decomposition import PCA
import Link_Prediction_Scores_All
#使用概率转移矩阵Te（node2vec边到边的概率转移）转换为节点相似性矩阵
#相似性矩阵Sij表示为 节点i到达节点j的概率pij + pji
#概率pij = Te中

DEAL_NAME = ['USAir']
def getProbabilityMatrix(adj,p,q):
   num_nodes = adj.shape[0]
   # S相似度矩阵
   adj_ = adj.toarray()
   S = np.zeros((num_nodes,num_nodes))
   for i in range(num_nodes):
      indexs = np.nonzero(adj_[i][:])[0]
      #pro_sum所有当前点在i的概率和
      #pro_corr所有当前点在i 下一步跳到j的概率 k为上一步 l为下一步
      pro_sum = 0
      for j in indexs:
            pro_sum = 0
            for k in indexs:
               pro_subSum = 0
               pro_subCorr = 0
               for l in indexs:
                  if(k==l):
                     pro = 1/p
                  elif(adj_[k][l]==1):
                     pro = 1
                  else:
                     pro = 1/q
                  pro_subSum = pro_subSum+pro
                  if(j==l):
                     pro_subCorr = pro
               pro_sum = pro_sum+pro_subCorr/pro_subSum
            S[i][j] = pro_sum/len(indexs)
            if(S[j][i]!=0):
               S[i][j] = S[i][j]+S[j][i]
               S[j][i] = S[i][j]
   return S

def pca_probabilityMatrix(M,components):
   pca = PCA(n_components=components)
   pca.fit(M)
   newM = pca.fit_transform(M)
   variance_ratio_ = pca.explained_variance_ratio_
   print(pca.explained_variance_ratio_)
   print(np.sum(variance_ratio_))
   return newM

prefix_name = 'D:/data/similarity_matrix/node2vec/'



#保存相似矩阵和pca后的
def save_Smatrix_newS(adj,dela_name,components):
   p = 0.5
   q = 2
   ori_Smatirx_name = dela_name + '-ori-{}-{}.pkl'.format(p,q)
   pca__Smatirx_name = dela_name + '-pca-{}.pkl'.format(components)
   file_name = prefix_name + ori_Smatirx_name
   if(not os.path.exists(file_name)):
      S = getProbabilityMatrix(adj, p, q)
      with open(file_name, 'wb') as f:
        pickle.dump(S, f, protocol=2)
      print(file_name+"S orimatrix saved!")
   else:
      with open(file_name, 'rb') as f:
         S = pickle.load(f)
   newS = pca_probabilityMatrix(S, components)
   file_name = prefix_name + pca__Smatirx_name
   with open(file_name, 'wb') as f:
      pickle.dump(newS, f, protocol=2)
   print(file_name+"S pcamatrix saved!")


if __name__ == '__main__':
   # components=[128,132,135,140,145,150,155,160]
   # for dela_name in DEAL_NAME:
   #    combined_dir = 'D:/data-processed/{}-adj.pkl'.format(dela_name)
   #    with open(combined_dir, 'rb') as f:
   #       adj = pickle.load(f)
   #       for component in components:
   #           save_Smatrix_newS(adj,dela_name,component)
   # pca_Smatirx_name = 'Oberlin-pca.pkl'
   # with open(file_name, 'rb') as f:
   #    S = pickle.load(f)
   # file_name = prefix_name + pca_Smatirx_name
   # with open(file_name, 'rb') as f:
   #    newS = pickle.load(f)
   # print('')
   pca_probabilityMatrix([[2,4,3,1],[5,8,7,2],[0,1,6,9],[2,0,3,7]],3)



